package org.custom.spring.boot.security.filters;

import cn.hutool.core.util.StrUtil;
import org.custom.spring.boot.security.filters.abstracts.AbstractUnityCommonFilter;
import org.custom.spring.boot.security.token.TokenManager;
import org.custom.spring.boot.security.utils.SecurityUtil;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.FilterChain;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 刷新 Token 信息的过滤器
 *
 * @author LiKun
 * @date 2021/11/11 23:31
 */
@Component("securityTokenDisposeFilter")
public class SecurityTokenDisposeFilter extends AbstractUnityCommonFilter {
    @Resource
    private TokenManager redisTokenManager;

    @Override
    protected boolean requestFilterLogicalProcess(HttpServletRequest request, HttpServletResponse response, FilterChain chain) {
        String tokenKey = SecurityUtil.getCredentials();

        // 请求用户不是 匿名用户
        if (StrUtil.isNotEmpty(tokenKey)) {
            redisTokenManager.refreshToken(tokenKey, request.getRequestURI());
        }

        return true;
    }
}
